#include "viewlogsdialog.h"
#include "ui_viewlogsdialog.h"
#include <QPaintEvent>
#include <QPainter>
#include <QGraphicsDropShadowEffect>
#include <QHBoxLayout>
#include "globaldef.h"
#include "project.h"
#include <QDir>
#include "qterminal.h"

ViewLogsDialog::ViewLogsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ViewLogsDialog)
{
    ui->setupUi(this);

    initWidget();
}

ViewLogsDialog::~ViewLogsDialog()
{
    delete ui;
}

void ViewLogsDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void ViewLogsDialog::showEvent(QShowEvent *)
{
    this->activateWindow();
}

void ViewLogsDialog::show(const QString &logdir)
{
    m_logdir = logdir;
    QStringList files = QDir(logdir).entryList(QStringList() << "*.log", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDir::Name);

    fillTableData(files);
    exec();
}

void ViewLogsDialog::initWidget()
{
    if (windowWidth >= 1280) {
        this->setGeometry(0,0,1280,800);
        this->setProperty("cssSize", "large");
    }
    this->setParent(Project::Instance());
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    setWindowFlags(windowFlags() | Qt::SubWindow);

    initTableHeader();
}

void ViewLogsDialog::initTableHeader()
{
    // 设置列宽
    ui->tableWidget->setColumnWidth(0, 80);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    // 设置表头行高
    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

    // 隐藏行标题
    ui->tableWidget->verticalHeader()->setHidden(true);
}

void ViewLogsDialog::fillTableData(const QStringList &logfiles)
{
    ui->tableWidget->setRowCount(logfiles.count());

    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        int rowHeight = 50;
        ui->tableWidget->setRowHeight(i, rowHeight);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i + 1)));
        ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignCenter);
        QWidget * box = new QWidget;
        QHBoxLayout * layout = new QHBoxLayout;
        QLabel * package = new QLabel;
        QLabel * checkImage = new QLabel;

        checkImage->setMinimumSize(26, 21);
        checkImage->setMaximumSize(26, 21);

        package->setText(logfiles.at(i));

        layout->addWidget(package);
        layout->addWidget(checkImage);
        layout->setAlignment(Qt::AlignCenter);
        box->setLayout(layout);
        ui->tableWidget->setCellWidget(i, 1, box);

        // 默认选中最后一行
        setSelectedRowSign(i, i == ui->tableWidget->rowCount() - 1);
    }

    // 滚动到最后一行
    ui->tableWidget->scrollToBottom();
}

void ViewLogsDialog::setSelectedRowSign(int row, bool show)
{
    QWidget *box = ui->tableWidget->cellWidget(row, 1);
    QList<QLabel *> labels = box->findChildren<QLabel *>();
    if (labels.count() > 1)
    {
        if (show)
        {
            m_selectedLogfile = labels.at(0)->text().trimmed();
            labels.at(1)->setStyleSheet("border-image: url(:/resources/check_success.png);");
        }
        else
        {
            labels.at(1)->setStyleSheet("");
        }
    }
}

void ViewLogsDialog::on_tableWidget_cellClicked(int row, int column)
{
    Q_UNUSED(column)
    int rowCount = ui->tableWidget->rowCount();
    for (int i = 0; i < rowCount; i++)
    {
        setSelectedRowSign(i, row == i);
    }
}


void ViewLogsDialog::on_btnOpenSelected_clicked()
{
    QTerminal terminal;
    terminal.run(QString("gedit %1/%2").arg(m_logdir).arg(m_selectedLogfile));
}

void ViewLogsDialog::on_btnExportSelected_clicked()
{
}

void ViewLogsDialog::on_btnExportAll_clicked()
{
}

void ViewLogsDialog::on_btnClose_clicked()
{
    done(0);
}
